import { defineStore } from 'pinia'
import axios from 'axios'
import { uuid } from 'vue-uuid'
import { reactive } from 'vue'
//   选项式写法：
// export const useLoveTalkListStore = defineStore('loveTalk', {
//   actions: {
//     // async getATalk() {
//     //   let {
//     //     data: { content:text },
//     //   } = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
//     //   let obj = { id: uuid.v4(), text }
//     //   this.talkList.unshift(obj)
//     // },
//     async getATalk() {
//       let result = await axios.get('/api/SweetNothings', {
//         headers: {
//           'Content-type': 'application/json',
//           Accept: 'application/json',
//         },
//       })
//       let obj = { id: uuid.v4(), text: result.data }
//       this.talkList.unshift(obj)
//     },
//   },
//   state() {
//     return {
//       talkList: JSON.parse(localStorage.getItem('loveTalk') as string) || [],
//     }
//   },
// })

//   组合式写法：
export const useLoveTalkListStore = defineStore('loveTalk', () => {
  const talkList = reactive(JSON.parse(localStorage.getItem('loveTalk') as string) || [])
  async function getATalk() {
    let {
      data: { content: text },
    } = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json')
    let obj = { id: uuid.v4(), text }
    talkList.unshift(obj)
  }
  return { talkList, getATalk }
})
